<?php
// Auto-rebuild the theme registry during theme development.
if (theme_get_setting('clear_registry')) {
  // Rebuild .info data.
  system_rebuild_theme_data();
  // Rebuild theme registry.
  drupal_theme_rebuild();
}
// Add Zen Tabs styles
if (theme_get_setting('basic_tabs')) {
  drupal_add_css( drupal_get_path('theme', 'basic') .'/css/tabs.css');
}
/**
 * Changes the default meta content-type tag to the shorter HTML5 version
 */
function cb_html_head_alter(&$head_elements) {
  $head_elements['system_meta_content_type']['#attributes'] = array(
    'charset' => 'utf-8'
  );
}

/**
 * Changes the search form to use the HTML5 "search" input attribute
 */
function cb_preprocess_search_block_form(&$vars) {
  $vars['search_form'] = str_replace('type="text"', 'type="search"', $vars['search_form']);
}

/**
 * Uses RDFa attributes if the RDF module is enabled
 * Lifted from Adaptivetheme for D7, full credit to Jeff Burnz
 * ref: http://drupal.org/node/887600
 */
function cb_preprocess_html(&$vars) {
  $vars['classes_array'][] = 'body';

  /* Since menu is rendered in preprocess_page we need to detect it here to add body classes */
  $has_main_menu = theme_get_setting('toggle_main_menu');
  $has_secondary_menu = theme_get_setting('toggle_secondary_menu');

  // Adding classes whether #navigation is here or not
  if ($has_main_menu or $has_secondary_menu) {
    $vars['classes_array'][] = 'with-navigation';
  }

  if ($has_secondary_menu) {
    $vars['classes_array'][] = 'with-subnav';
  }

  /* Add extra classes to body for advanced theming */

  if ($vars['is_admin']) {
    $vars['classes_array'][] = 'admin';
  }

	if (!$vars['is_front']) {
		// Add unique classes for each page and website section
		$path = drupal_get_path_alias($_GET['q']);
		list($section, ) = explode('/', $path, 2);
		$vars['classes_array'][] = cb_id_safe('page-'. $path);
		$vars['classes_array'][] = cb_id_safe('section-'. $section);
	}
}
/**
 * Implement of hook_proccess_html()
 */
function cb_process_html(&$vars) {
  $vars['page_bottom'] .= drupal_get_js('extra_footer');
}
/**
 * Preprocess_page
 */
function cb_preprocess_page(&$vars) {
  if ($vars['is_front']) {
    if (theme_get_setting('hide_frontpage_main_content')) {
      $vars['page']['content']['system_main'] = array();
    }
  }
  $vars['hide_site_name'] = theme_get_setting('hide_site_name');
  $n = menu_get_object();
  if (isset($vars['node_title'])) {
    $vars['title'] = $vars['node_title'];
  }
  if (drupal_match_path($_GET['q'], 'node/*') && !arg(2) && !empty($n) && ($n->type == 'project')) {
    $vars['title'] = "";
  }
}

/**
 * Implementation of phptemplate_preprocess_node().
 *
 * Adds extra classes to node container for advanced theming
 */
function cb_preprocess_node(&$vars) {
  global $language;
  // Striping class
  $vars['classes_array'][] = 'node-' . $vars['zebra'];

  // Node is published
  $vars['classes_array'][] = ($vars['status']) ? 'published' : 'unpublished';

  // Node has comments?
  $vars['classes_array'][] = ($vars['comment']) ? 'with-comments' : 'no-comments';

  if ($vars['sticky']) {
    $vars['classes_array'][] = 'sticky'; // Node is sticky
  }

  if ($vars['promote']) {
    $vars['classes_array'][] = 'promote'; // Node is promoted to front page
  }

  if ($vars['teaser']) {
    $vars['classes_array'][] = 'node-teaser'; // Node is displayed as teaser.
  }

  if ($vars['uid'] && $vars['uid'] === $GLOBALS['user']->uid) {
    $classes[] = 'node-mine'; // Node is authored by current user.
  }
}

function cb_preprocess_block(&$vars, $hook) {
  // Add a striping class.
  $vars['classes_array'][] = 'block-' . $vars['zebra'];
}

/**
 * Return a themed breadcrumb trail.
 *
 * @param $breadcrumb
 *   An array containing the breadcrumb links.
 * @return
 *   A string containing the breadcrumb output.
 */
function cb_breadcrumb($vars) {
  $breadcrumb = $vars['breadcrumb'];
  // Determine if we are to display the breadcrumb.
  $show_breadcrumb = theme_get_setting('breadcrumb_display');
  if ($show_breadcrumb == 'yes') {

    // Optionally get rid of the homepage link.
    $show_breadcrumb_home = theme_get_setting('breadcrumb_home');
    if (!$show_breadcrumb_home) {
      array_shift($breadcrumb);
    }

    // Return the breadcrumb with separators.
    if (!empty($breadcrumb)) {
      $separator = filter_xss(theme_get_setting('breadcrumb_separator'));
      $trailing_separator = $title = '';

      // Add the title and trailing separator
      if (theme_get_setting('breadcrumb_title')) {
        if ($title = drupal_get_title()) {
          $trailing_separator = $separator;
        }
      }
      // Just add the trailing separator
      elseif (theme_get_setting('breadcrumb_trailing')) {
        $trailing_separator = $separator;
      }

      // Assemble the breadcrumb
      return implode($separator, $breadcrumb) . $trailing_separator . $title;
    }
  }
  // Otherwise, return an empty string.
  return '';
}

/*
 * 	Converts a string to a suitable html ID attribute.
 *  Copied from "cb"
 *
 * 	 http://www.w3.org/TR/html4/struct/global.html#h-7.5.2 specifies what makes a
 * 	 valid ID attribute in HTML. This function:
 *
 * 	- Ensure an ID starts with an alpha character by optionally adding an 'n'.
 * 	- Replaces any character except A-Z, numbers, and underscores with dashes.
 * 	- Converts entire string to lowercase.
 *
 * 	@param $string
 * 	  The string
 * 	@return
 * 	  The converted string
 */
function cb_id_safe($string) {
  // Replace with dashes anything that isn't A-Z, numbers, dashes, or underscores.
  $string = strtolower(preg_replace('/[^a-zA-Z0-9_-]+/', '-', $string));
  // If the first character is not a-z, add 'n' in front.
  if (!ctype_lower($string{0})) { // Don't use ctype_alpha since its locale aware.
    $string = 'id'. $string;
  }
  return $string;
}

function tett($menu_item){
	$v2 ='';
	foreach ($menu_item['link']['localized_options'] as $v1) {
		foreach ($v1 as $v2) {

		};
	}
	return $v2;
}

function color_perm() {
  return array('access color content');
} // function onthisdate_perm()

/**
 * Implement of hook_js_alter
 */
function cb_js_alter(&$js) {
  foreach ($js as $name => $javascript) {
    // Move footer to really below the header scope
    if ($js[$name]['scope'] == 'footer') {
      $js[$name]['scope'] = 'extra_footer';
    }

    $exclude = FALSE;
    $preprocess = TRUE;

    // Ckeditor doesn't work in compress mode
    if (is_string($js[$name]['data']) && strstr($js[$name]['data'], 'ckeditor.js') && $js[$name]['type'] != 'inline') {
      $preprocess = FALSE;
    }

    // Add to any need to be exclude
    if ($js[$name]['type']== 'inline' && is_string($js[$name]['data']) && strstr($js[$name]['data'], 'da2a')) {
      $exclude = TRUE;
    }

    if (!$exclude) {
      // Change the weight with the scope -> group -> every_page -> weight Ref: http://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_add_js/7
      $js[$name]['weight'] = ($js[$name]['scope'] != 'header') * 10000
        + $js[$name]['group'] * 10  // $js[$name]['group'] is -100/0/100
        + (!$js[$name]['every_page']) * 100
        + $js[$name]['weight'];

      $js[$name]['scope'] = $js[$name]['scope'] != 'extra_footer' ? 'footer' : 'extra_footer';
      $js[$name]['group'] = JS_DEFAULT;
      $js[$name]['every_page'] = TRUE;
      $js[$name]['preprocess'] = $preprocess;
    }
  }
}

/**
 * Aggregate and compress CSS files
 */
function cb_css_alter(&$css) {
  // Sort CSS items, so that they appear in the correct order.
  // This is taken from drupal_get_css().
  uasort($css, 'drupal_sort_css_js');

  // The other style sheets
  // I will populate this array with the other than "All" css (usually I have only one but just in case�)
  $other = array();

  // I will add some weight to the new $css array so every element keeps its position
  $weight = 0;
  
  foreach ($css as $name => $style) {
    $css[$name]['group'] = CSS_DEFAULT;
    $css[$name]['preprocess'] = TRUE;
    $css[$name]['every_page'] = TRUE;
    // I move all the style sheets other than all to a new array
    if ($css[$name]['media'] != 'all' && $css[$name]['media'] != 'screen') {
      // remove and add to a new array
      $other[$css[$name]['media']][$name] = $css[$name];           
      unset($css[$name]);
      continue;
    }
    // I leave untouched the conditional stylesheets
    // and put all the rest inside a 0 group
    if ($css[$name]['browsers']['!IE']) {
      $css[$name]['weight'] = ++$weight;
      $css[$name]['media'] != 'all';
    }
  }

  // I merge the regular array and the other css array
  foreach ($other as $key => $val) {
    //rearrange weight continuous in group
    foreach ($val as &$v) {
      $v['weight'] = ++$weight;
    }
    //arranged by weight css array
    $css = array_merge($css, $val);
  }
}
